<?php

define( 'MODULE_PATH', 'module/' );
define( 'DEFAULT_TEMPLATE_PATH', 'layout/default/index.php' );
define( 'PHP_IDENT', "\t" );

class System_App_Manager_Vue extends System_App_Noyau{
    public $title;
    public $siteBaseUrl;
    public $aMeta;
    public $aScript;
    public $aStyle;
    public $config;
    
    private $_layout;
    private $_page;
    
    public $content;
    public $view;
    
    public function __construct(){ 
        $this->aMeta = array();
        $this->aScript = array();
        $this->aStyle = array();
        $this->config = $this->getConfig('general');
    }
    
    private function _buildView(){
        $this->_layout = new System_App_Manager_Layout();
        $this->_page = new System_App_Manager_Page();
        
        $this->setSiteBaseUrl();
        $this->setHttpEquiv('Content-Type', $this->config['content-type'] . '; charset=' . $this->config['encodage'] );
        $this->setHttpEquiv('Content-Language', $this->config['langue'] );
        
        $this->content = $this->_getTemplate( $this->_page->getPath() );        
        $this->view = $this->_getTemplate( $this->_layout->getPath() );
    }
    
    private function _getTemplate( $path ){
        if( file_exists( $path ) ){
            ob_start();
            include ( $path );
            echo PHP_EOL;
            return ob_get_clean();
        }else{
            System_App_Erreur::page404();
        }
    }
    
    /* definition du titre de page */
    public function getTitle(){
        return PHP_IDENT . '<title>' . $this->title . '</title>'.PHP_EOL;
    }
    public function setTitle( $value ){
        $this->title = $value;
    }
    
    public function setSiteBaseUrl( $url = '' ){
        if( $url !== '' ){
            $this->siteBaseUrl = $url;
        }else{
            $this->siteBaseUrl = $_SERVER['SERVER_NAME'];
        }
    }
    
    public function getSiteBaseUrl(){
        if( $this->siteBaseUrl ){
            return PHP_IDENT . '<base href="http://' . $this->siteBaseUrl . '/" >' . PHP_EOL;
        }else{
            return '';
        }
    }
    public function getHtmlLangAttr(){
        return ' lang="' . $this->config['langue'] . '" ';
    }
    
    /* gestion metas */
    /* http-equiv */
    public function setHttpEquiv( $name, $value ){
        $meta = PHP_IDENT . '<meta http-equiv="' . $name . '" content="' . $value . '">';
        array_unshift( $this->aMeta, $meta);
        return $this;
    }
    /* favicon */
    public function setFavIcon($url, $type='image/x-icon'){
        $favicon = PHP_IDENT . '<link rel="shortcut icon" type="' . $type . '" href="' . $url . '" />';
        array_push( $this->aStyle, $favicon );
        return $this;
    }
    public function appendMeta($name, $value){
        $meta = PHP_IDENT . '<meta name="' . $name . '" content="' . $value . '" >';
        array_push($this->aMeta, $meta);
        return $this;
    }
    public function prependMeta($name, $value){
        $meta = PHP_IDENT . '<meta name="' . $name . '" content="' . $value . '" >';
        array_unshift( $this->aMeta, $meta);
        return $this;
    }
    public function getMetas(){
        if( is_array($this->aMeta) && count($this->aMeta) > 0 ){
            return $this->getTitle() . $this->getSiteBaseUrl() . implode( PHP_EOL, $this->aMeta ).PHP_EOL;
        }
    }
    
    /* gestion styles */
    public function appendStyle($path){
        $style = PHP_IDENT . '<link type="text/css" href="' . $path . '" rel="stylesheet" />';
        array_push( $this->aStyle, $style );
        return $this;
    }
    public function prependStyle($path){
        $style = PHP_IDENT . '<link type="text/css" href="' . $path . '" rel="stylesheet" />';
        array_unshift( $this->aStyle, $style );
        return $this;
    }
    public function getStyles(){
        if( is_array($this->aStyle) && count($this->aStyle) > 0 ){
            return implode(PHP_EOL, $this->aStyle );
        }
    }
    
    /* gestion scripts */
    public function appendScript($path){
        $script = PHP_IDENT . '<script type="text/javascript" src="' . $path . '"></script>';
        array_push( $this->aScript, $script );
        return $this;
    }
    public function prependScript($path){
        $script = PHP_IDENT . '<script type="text/javascript" src="' . $path . '"></script>';
        array_unshift( $this->aScript, $script );
        return $this;
    }
    public function getScripts(){
        if( is_array($this->aScript) && count($this->aScript) > 0 ){
            return implode(PHP_EOL, $this->aScript );
        }
    }
    
    /* recuperation de la vue construite */
    public function getView(){
        $this->_buildView();
        return $this->view;
    }    
}
?>
